package com.kuhyygy.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kuhyygy.R;
import com.kuhyygy.bean.Emotion;

/**
 * 自定义的Adapter
 * 一、要构造方法；
 * 二、重写getView方法
 * 
 * 
 * @author acer
 *
 */
public class EmotionAdapter extends ArrayAdapter<Emotion> {
	
	private int resource;

	/**
	 * 
	 * @param context	父的Activity
	 * @param resource	ListView的Layout
	 * @param objects
	 */
	public EmotionAdapter(Context context, int resource, List<Emotion> objects) {
		super(context, resource, objects);
		this.resource = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Emotion e = getItem(position);
		View view;
		ViewHolder viewHolder;
		if (null == convertView) {	// 布局不重复加载
			view = LayoutInflater.from(getContext()).inflate(resource, null);
			viewHolder = new ViewHolder();
			viewHolder.imageViewHolder = (ImageView) view.findViewById(R.id.emotionImg);
			viewHolder.textViewHolder = (TextView) view.findViewById(R.id.emotionText);
			view.setTag(viewHolder);
		} else {
			view = convertView;
			viewHolder = (ViewHolder) view.getTag();
		}
		viewHolder.imageViewHolder.setImageResource(e.getId());
		viewHolder.textViewHolder.setText(e.getDesc());
		return view;
	}
	
	/**
	 * 控件不重复加载
	 * @author acer
	 *
	 */
	class ViewHolder {
		ImageView imageViewHolder;
		TextView textViewHolder;
		public ImageView getImageViewHolder() {
			return imageViewHolder;
		}
		public void setImageViewHolder(ImageView imageViewHolder) {
			this.imageViewHolder = imageViewHolder;
		}
		public TextView getTextViewHolder() {
			return textViewHolder;
		}
		public void setTextViewHolder(TextView textViewHolder) {
			this.textViewHolder = textViewHolder;
		}
	}

}
